Regions Cut by Slashes

Try to solve the Regions Cut By Slashes problem.

Statement#

An n×nn \times n grid is composed of nn, 1×11 \times 1 squares, where each 1×11 \times 1 square consists of a “/”, “\”, or a blank space. These characters divide the square into adjacent regions.

Given the grid represented as a string array, return the number of adjacent regions.

Note:

  1. Backslash characters are escaped, so “\” is represented as “\\”.
  2. A 1×11 \times 1 square in the grid will be referred to as a box.

Constraints:

  • The grid consists of only the “/”, “\”, or " " characters.
  • 1 \leq grid.length \leq 30

The following illustration shows how the grid can be visualized:

svg viewer

1 of 16

svg viewer

2 of 16

svg viewer

3 of 16

svg viewer

4 of 16

svg viewer

5 of 16

svg viewer

6 of 16

svg viewer

7 of 16

svg viewer

8 of 16

svg viewer

9 of 16

svg viewer

10 of 16

svg viewer

11 of 16

svg viewer

12 of 16

svg viewer

13 of 16

svg viewer

14 of 16

svg viewer

15 of 16

svg viewer

16 of 16

Examples#

svg viewer

1 of 3

svg viewer

2 of 3

svg viewer

3 of 3

Understand the problem#

Let’s take a moment to make sure you’ve correctly understood the problem. The quiz below helps you check if you’re solving the correct problem:

Regions Cut By Slashes

3

What is the output if the following grid is given as input?

grid = ["  /", “/ /”, "\\/ "]

Your Answer
A)

1

B)

2

Correct Answer
C)

3

Explanation
There are three regions in this grid.
D)

4

Question 3 of 33 attempted

Figure it out!#

We have a game for you to play. Rearrange the logical building blocks to develop a clearer understanding of how to solve this problem.

Drag and drop the cards to rearrange them in the correct sequence.

Divide each box in the n×nn \times n grid into four regions: north, south, west, and east.

Traverse each box and combine the regions in the box based on the input character.

Connect the current box with its top, bottom, left, and right neighboring boxes.

Repeat this process until the entire grid has been traversed.

Count the number of connected components that represent the regions in the grid.


Try it yourself#

Implement your solution in main.py in the following coding playground. You will need the provided supporting code to implement your solution.

Python
main.py
union_find.py
Powered by AI
Input #1
Regions Cut by Slashes

Solution: Last Day Where You Can Still Cross

Solution: Regions Cut by Slashes